package com.vd.wmt.model;

import java.math.BigDecimal;
import java.util.List;

import javax.swing.event.EventListenerList;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class WageTableModel implements TableModel {

	
	private List<? extends WageInfo> wageList;

	public List<? extends WageInfo> getWageList() {
		return wageList;
	}

	public void setWageList(List<? extends WageInfo> wageList) {
		this.wageList = wageList;
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == TableModelListener.class) {
				((TableModelListener) listeners[i + 1])
						.tableChanged(new TableModelEvent(this));
			}
		}
	}

	private String[] columns = new String[] { "姓名", "邮箱", "基本工资", "基本奖金",
			"外派补贴", "中餐补贴", "交通补贴", "上班天数", "其他补贴", "其它补扣款", "迟到扣款", "个税",
			"社保", "公积金", "实发工资", "中行", "其它行","邮件状态" };

	/** List of listeners */
	protected EventListenerList listenerList = new EventListenerList();

	@Override
	public void addTableModelListener(TableModelListener l) {
		listenerList.add(TableModelListener.class, l);
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex < 2 ||columnIndex == columns.length-1 ) {
			return String.class;
		} {
			return BigDecimal.class;
		}
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return columns[columnIndex];
	}

	@Override
	public int getRowCount() {
		if (wageList == null) {
			return 0;
		} else {
			return wageList.size();
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		WageInfo wi = wageList.get(rowIndex);
		if (columnIndex == 0) {
			return wi.getUserName();
		} else if (columnIndex == 1) {
			return wi.getEmail();
		} else if (columnIndex == 2) {
			return wi.getBasicWage();
		} else if (columnIndex == 3) {
			return wi.getBasicBonus();
		} else if (columnIndex == 4) {
			return wi.getExpatriateSubsidy();
		} else if (columnIndex == 5) {
			return wi.getMealSubsidy();
		} else if (columnIndex == 6) {
			return wi.getTransportSubsidy();
		} else if (columnIndex == 7) {
			return wi.getAttendanceDays();
		} else if (columnIndex == 8) {
			return wi.getOtherSusidy();
		} else if (columnIndex == 9) {
			return wi.getOtherCompensationDeduction();
		} else if (columnIndex == 10) {
			return wi.getLateCharge();
		} else if (columnIndex == 11) {
			return wi.getPersonIncomeTax();
		} else if (columnIndex == 12) {
			return wi.getSocialSecurityFund();
		} else if (columnIndex == 13) {
			return wi.getHousingFund();
		} else if (columnIndex == 14) {
			return wi.getRealWage();
		} else if (columnIndex == 15) {
			return wi.getWageInBOC();
		} else if (columnIndex == 16) {
			return wi.getWageInPA();
		} else if(columnIndex == 17){
			return wi.getSendStatus();
		}
		return null;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		listenerList.remove(TableModelListener.class, l);
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		
		wageList.get(rowIndex).setSendStatus(String.valueOf(value));
		TableModelEvent event =new TableModelEvent(this,rowIndex);
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == TableModelListener.class) {
				((TableModelListener) listeners[i + 1])
						.tableChanged(event);
			}
		}
	}

}
